package com.hrzyit.whistle.comm.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.comm.R
import com.hrzyit.whistle.comm.data.bean.ContactEntity
import com.hrzyit.whistle.comm.presenter.ContactsPresenter
import com.hrzyit.whistle.comm.presenter.view.ContactsView
import com.hrzyit.whistle.comm.ui.adapter.ContactsAdapter
import kotlinx.android.synthetic.main.activity_contact.*
import me.yokeyword.indexablerv.IndexableLayout
import java.util.*

/**
 * 通讯录界面
 */
//@Route(path = RouterPath.CommunicationCenter.PATH_COMM_CONTACTS)
class ContactActivity : BaseMvpActivity<ContactsPresenter>(), ContactsView {
    override fun onGetSelectContactsListSuccess(result: MutableList<ContactEntity>) {
    }

    companion object {
       const val TAG = "ContactActivity"
   }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contact)

        initView()

        initAdapter()
    }

    private fun initView(){

    }


    override fun injectComponent() {

    }

    private fun initAdapter() {

        // 初始化联系人索引选择器
        val indexLayout = contactIndexLayout as IndexableLayout
        indexLayout.setLayoutManager(LinearLayoutManager(this))

        // setAdapter
        val mAdapter = ContactsAdapter(this)
        indexLayout.setAdapter(mAdapter)
        // set Data
        mAdapter.setDatas(initDataList())

        // set Material Design OverlayView
        indexLayout.setOverlayStyle_MaterialDesign(Color.BLUE)

        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexLayout.setCompareMode(IndexableLayout.MODE_FAST)

        // set Listener
        mAdapter.setOnItemContentClickListener { _, _, _, entity ->
            // 查看联系人详情
            Log.d(TAG, "$entity")
            ARouter.getInstance().build(RouterPath.UserCenter.PATH_USER_INFO).navigation()
        }
        mAdapter.setOnItemTitleClickListener { _, currentPosition, indexTitle ->
            Log.d(TAG,"选中:$indexTitle  当前位置:$currentPosition")
        }
    }

    private fun initDataList(): List<ContactEntity> {
        val list = ArrayList<ContactEntity>()
        // 初始化数据
        // TODO 从数据库中加载数据源
        val contactStrings = Arrays.asList(*resources.getStringArray(R.array.contact_array))
        var contactEntity:ContactEntity?
        for (i in contactStrings.indices) {
            contactEntity = ContactEntity()
            contactEntity.name = contactStrings[i]
            list.add(contactEntity)
        }
        return list
    }
}
